package ua.ck.example.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import ua.ck.example.domain.Employee;

@Repository
public class EmployeeDAOImpl implements EmployeeDAO {

	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public void addEmployee(Employee employee) {
		sessionFactory.getCurrentSession().save(employee);
	}

	@Override
	public Employee getEmployee(Employee employee) {
		StringBuffer sql = new StringBuffer("FROM Employee e WHERE 1=1");
		sql.append(" and e.empName='" + employee.getEmpName() + "'");
		sql.append(" and e.password='" + employee.getPassword() + "'");
		List<Employee> employeeList = (List<Employee>) sessionFactory
				.getCurrentSession().createQuery(sql.toString()).list();
		if (employeeList != null && employeeList.size() > 0) {
			return employeeList.get(0);
		}
		return null;
	}

}
